package com.example.httpmode;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class SettingActivity2 extends AppCompatActivity implements View.OnClickListener {

    private View setting_iv_exit;
    private ImageView setting_iv_save;
    private ImageView setting_iv_exits;
    private SharedPreferences config;
    private EditText setting_ed_ip;
    private EditText setting_ed_port;
    private EditText setting_ed_room_gateway;
    private EditText setting_ed_kitchen_gateway;
    private EditText setting_ed_bedroom1_gateway;
    private EditText setting_ed_bedroom2_gateway;
    private EditText setting_ed_toilet_gateway;
    private EditText setting_ed_temp;
    private EditText setting_ed_hum;
    private EditText setting_ed_light;
    private EditText setting_ed_body;
    private EditText setting_ed_bodys;
    private EditText setting_ed_flame;
    private EditText setting_ed_smoke;
    private EditText setting_ed_police;
    private EditText setting_ed_fan;
    private EditText setting_ed_lamp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting2);

        setting_iv_exit = findViewById(R.id.setting_iv_exit);
        setting_iv_save = findViewById(R.id.setting_iv_save);
        setting_iv_exits = findViewById(R.id.setting_iv_exits);
        setting_ed_ip = findViewById(R.id.setting_ed_ip);
        setting_ed_port = findViewById(R.id.setting_ed_port);

        setting_ed_room_gateway = findViewById(R.id.setting_ed_room_gateway);
        setting_ed_kitchen_gateway = findViewById(R.id.setting_ed_kitchen_gateway);
        setting_ed_bedroom1_gateway = findViewById(R.id.setting_ed_bedroom1_gateway);
        setting_ed_bedroom2_gateway = findViewById(R.id.setting_ed_bedroom2_gateway);
        setting_ed_toilet_gateway = findViewById(R.id.setting_ed_toilet_gateway);
        setting_ed_temp = findViewById(R.id.setting_ed_temp);
        setting_ed_hum = findViewById(R.id.setting_ed_hum);
        setting_ed_light = findViewById(R.id.setting_ed_light);
        setting_ed_body = findViewById(R.id.setting_ed_body);
        setting_ed_bodys = findViewById(R.id.setting_ed_bodys);
        setting_ed_flame = findViewById(R.id.setting_ed_flame);
        setting_ed_smoke = findViewById(R.id.setting_ed_smoke);
        setting_ed_police = findViewById(R.id.setting_ed_police);
        setting_ed_fan = findViewById(R.id.setting_ed_fan);
        setting_ed_lamp = findViewById(R.id.setting_ed_lamp);


        setting_iv_exit.setOnClickListener(this);
        setting_iv_save.setOnClickListener(this);
        setting_iv_exits.setOnClickListener(this);

        config = getSharedPreferences("config", MODE_PRIVATE);
        if(config != null && !"".equals(config)){

            //从config文件中获取保存的配置信息
            String ip = config.getString("ip", "");
            String port = config.getString("port", "");
            String setting_room_gateway = config.getString("room_gateway", "");
            String setting_kitchen_gateway = config.getString("kitchen_gateway", "");
            String setting_bedroom1_gateway = config.getString("bedroom1_gateway", "");
            String setting_bedroom2_gateway = config.getString("bedroom2_gateway", "");
            String setting_toilet_gateway = config.getString("toilet_gateway", "");
            String setting_temp = config.getString("setting_temp", "");
            String setting_hum = config.getString("setting_hum", "");
            String setting_light = config.getString("setting_light", "");
            String setting_body = config.getString("setting_body", "");
            String setting_bodys = config.getString("setting_bodys", "");
            String setting_flame = config.getString("setting_flame", "");
            String setting_smoke = config.getString("setting_smoke", "");
            String setting_police = config.getString("setting_police", "");
            String setting_fan = config.getString("setting_fan", "");
            String setting_lamp = config.getString("setting_lamp", "");

            //将获取的配置信息设置到ui界面上
            setting_ed_ip.setText(ip);
            setting_ed_port.setText(port);
            setting_ed_room_gateway.setText(setting_room_gateway);
            setting_ed_kitchen_gateway.setText(setting_kitchen_gateway);
            setting_ed_bedroom1_gateway.setText(setting_bedroom1_gateway);
            setting_ed_bedroom2_gateway.setText(setting_bedroom2_gateway);
            setting_ed_toilet_gateway.setText(setting_toilet_gateway);
            setting_ed_temp.setText(setting_temp);
            setting_ed_hum.setText(setting_hum);
            setting_ed_light.setText(setting_light);
            setting_ed_body.setText(setting_body);
            setting_ed_bodys.setText(setting_bodys);
            setting_ed_flame.setText(setting_flame);
            setting_ed_smoke.setText(setting_smoke);
            setting_ed_police.setText(setting_police);
            setting_ed_fan.setText(setting_fan);
            setting_ed_lamp.setText(setting_lamp);
        }

    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.setting_iv_exit:
            case R.id.setting_iv_exits:
                finish();
                overridePendingTransition(0,0);
                break;
            case R.id.setting_iv_save:
                //监听，获取ui界面上的文本
                String ipstr = setting_ed_ip.getText().toString();
                String portstr = setting_ed_port.getText().toString();
                String room_gatewaystr = setting_ed_room_gateway.getText().toString();
                String kitchen_gatewaystr = setting_ed_kitchen_gateway.getText().toString();
                String bedroom1_gatewaystr = setting_ed_bedroom1_gateway.getText().toString();
                String bedroom2_gatewaystr = setting_ed_bedroom2_gateway.getText().toString();
                String toilet_gatewaystr = setting_ed_toilet_gateway.getText().toString();
                String tempstr = setting_ed_temp.getText().toString();
                String humstr = setting_ed_hum.getText().toString();
                String lightstr = setting_ed_light.getText().toString();
                String bodystr = setting_ed_body.getText().toString();
                String bodysstr = setting_ed_bodys.getText().toString();
                String flamestr = setting_ed_flame.getText().toString();
                String smokestr = setting_ed_smoke.getText().toString();
                String policestr = setting_ed_police.getText().toString();
                String fanstr = setting_ed_fan.getText().toString();
                String lampstr = setting_ed_lamp.getText().toString();

                SharedPreferences.Editor edit = config.edit();
                edit.putString("ip",ipstr);
                edit.putString("port",portstr);
                edit.putString("room_gateway",room_gatewaystr);
                edit.putString("kitchen_gateway",kitchen_gatewaystr);
                edit.putString("bedroom1_gateway",bedroom1_gatewaystr);
                edit.putString("bedroom2_gateway",bedroom2_gatewaystr);
                edit.putString("toilet_gateway",toilet_gatewaystr);
                edit.putString("setting_temp",tempstr);
                edit.putString("setting_hum",humstr);
                edit.putString("setting_light",lightstr);
                edit.putString("setting_body",bodystr);
                edit.putString("setting_bodys",bodysstr);
                edit.putString("setting_flame",flamestr);
                edit.putString("setting_smoke",smokestr);
                edit.putString("setting_police",policestr);
                edit.putString("setting_fan",fanstr);
                edit.putString("setting_lamp",lampstr);
                edit.commit();
                break;
        }
    }
}